/*
 * vim:et sts=4 sw=4 cindent:
 * $Id$
 */

class com.stamen.utils.Timer extends mx.events.EventDispatcher
{
    public static var EVENT_TICK:String = 'tick';

    private var _rate:Number;
    private var _intervalID:Number;
    private var _running:Boolean = false;

    public function Timer(rate:Number)
    {
        _rate = rate;
    }

    public function get rate():Number
    {
        return _rate;
    }

    public function set rate(value:Number):Void
    {
        _rate = value;
        if (_running)
        {
            reset();
        }
    }

    public function start():Void
    {
        if (!_running)
        {
            _running = true;
            _intervalID = setInterval(this, "tick", _rate);
        }
    }

    public function stop():Void
    {
        if (_running)
        {
            _running = false;
            clearInterval(_intervalID);
        }
    }

    public function reset():Void
    {
        stop();
        start();
    }

    private function tick():Void
    {
        dispatchEvent({type: EVENT_TICK});
    }
}
